<?php
/**
 * Created by PhpStorm.
 * User: DELL-PC
 * Date: 2018/9/4
 * Time: 12:59
 */

namespace Application\Common\Controller;


use J\Exception;
use J\Intent;

class AppendElementUIActor extends ManyElementUIActor
{

    /**
     * 扩展动作生成器
     *
     * @inheritdoc
     */
    protected function invokeIntentMethod(Intent $intent)
    {
        if ($this->controller->getInformation($intent, 'append', false) !== false) {
            if (__OPTION__ === 'ElementUI') {
                $this->optionElementUIAppend($intent);
                return null;
            }

            if (__OPTION__ === 'Api') {
                $this->optionApiAppend($intent);
                return null;
            }

            return false;
        }

        return parent::invokeIntentMethod($intent);
    }

    /**
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionElementUIAppend(Intent $intent)
    {
        $id = $this->findId();
        $row = $this->getModel()->getCurd()->setids($id)->find();
        $config = $this->controller->getInformation($intent);
        $mate = ElementUI::getView($this->controller, $intent, $config, $this->getModel());
        $key = $this->getModel()->getAttribute()->get('key');
        $mate['title'] = $row[$key] . '扩展';
        $intent->value = $mate;
    }

    /**
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiAppend(Intent $intent)
    {
        $id = $this->findId();
        $pid = $this->getModel()->getTreeKey();
        $elements = [$pid => $id, 'status' => 1];
        $this->getModel()->getCurd()->setElements($this->filterAdd($elements))->add();
        $this->next($intent, '添加成功');
    }
}